# -*- coding:utf-8 -*-

from common.import_pyside import *


class CustomTitleBar(QFrame):
    def __init__(self, parent):
        super().__init__(parent)
        self.setFixedHeight(40)
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0, 0, 0, 0)
        self.setLayout(self.layout)

        self.title = QLabel("自定义标题栏")
        self.layout.addWidget(self.title)

        self.minimizeButton = QPushButton("-")
        self.layout.addWidget(self.minimizeButton)

        self.maximizeButton = QPushButton("口")
        self.layout.addWidget(self.maximizeButton)

        self.closeButton = QPushButton("x")
        self.layout.addWidget(self.closeButton)

        self.minimizeButton.clicked.connect(self.parent().showMinimized)
        self.maximizeButton.clicked.connect(self.toggleMaximize)
        self.closeButton.clicked.connect(self.parent().close)

        self.setStyleSheet("""
            QFrame {
                background-color: #333;
                border: 1px solid black;
                border-top-left-radius: 5px;
                border-top-right-radius: 5px;
            }

            QLabel {
                color: white;
                font-size: 16px;
                padding-left: 10px;
            }

            QPushButton {
                background-color: #333;
                color: white;
                border: none;
                font-size: 18px;
                width: 30px;
                height: 30px;
                border-radius: 15px;
            }

            QPushButton:hover {
                background-color: #666;
                cursor: pointer;
            }
        """)
        self.dragPos = None

    # def mousePressEvent(self, event):
    #     self.dragPos = event.globalPos()
    #
    # def mouseMoveEvent(self, event):
    #     if self.dragPos:
    #         self.parent().move(event.globalPos() - self.dragPos)
    #         event.accept()
    #
    # def mouseReleaseEvent(self, event):
    #     self.dragPos = None

    def mousePressEvent(self, event):  ##事件开始
        if event.button() == Qt.LeftButton:
            self.Move = True  ##设定bool为True
            self.Point = event.globalPos() - self.pos()  ##记录起始点坐标
            event.accept()

    def mouseMoveEvent(self, QMouseEvent):  ##移动时间
        if Qt.LeftButton and self.Move:  ##切记这里的条件不能写死，只要判断move和鼠标执行即可！
            self.move(QMouseEvent.globalPos() - self.Point)  ##移动到鼠标到达的坐标点！
            QMouseEvent.accept()

    def mouseReleaseEvent(self, QMouseEvent):  ##结束事件
        self.Move = False

    def toggleMaximize(self):
        if self.parent().isMaximized():
            self.parent().showNormal()
        else:
            self.parent().showMaximized()


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("自定义标题栏")
        self.setWindowFlags(Qt.FramelessWindowHint)

        self.titleBar = CustomTitleBar(self)
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.titleBar)
        self.setLayout(self.layout)


if __name__ == '__main__':
    app = QApplication()
    window = MainWindow()
    window.show()
    app.exec()
